/**
 * @file example_007.cpp
 * @author yangjian (1171267147@qq.com)
 * @brief 编译期求值确定实例化分支
 * @version 0.1
 * @date 2025-07-14
 * 
 * @copyright Copyright (c) 2025
 * 
 */
#include <iostream>
#include <type_traits>

class Test1 {
public:
    void func1() { std::cout << "Test1::func1()" << '\n'; }
};

class Test2 {
public:
    void func2() { std::cout << "Test2::func2()" << '\n'; }
};

#if 0
template <typename T>
void func(T t) {
    if (typeid(T) == typeid(Test1))  // 运行期识别，不管是否为true，都会实例化分支代码导致编译失败
    {
        t.func1();
    } else if (typeid(T) == typeid(Test2))
    {
        t.func2();
    }
}
#elif __cplusplus >= 201703L
template <typename T>
void func(T t) {
    if constexpr (std::is_same_v<T, Test1>)  // 编译期求值，确定需要实例化的分支
    {
        t.func1();
    } else if constexpr (std::is_same_v<T, Test2>) {
        t.func2();
    }
}
#endif

int main() {
    Test1 t1;
    Test2 t2;
    func(t1);
    func(t2);
}